/**
 * 
 */
package ro.dta.idbi.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import ro.dta.idbi.interfaces.ApplicationSections;
import ro.dta.idbi.interfaces.ui.IMainFrame;
import ro.dta.idbi.model.Utils;

/**
 * Panel displayed when the user first opens the application
 * 
 * @author Tudor Dobrila
 */
public class FirstScreenPanel extends JGradientPanel implements ActionListener {

	private static final long serialVersionUID = -1853715192110232600L;

	private static final int PADDING = 5;

	protected transient IMainFrame parentFrame;

	/**
	 * GeneralPanel class constructor
	 */
	public FirstScreenPanel(final IMainFrame parentFrame) {
		super();
		this.parentFrame = parentFrame;
		setLayout(new GridBagLayout());

		final EmptyBorder border1 = new EmptyBorder(PADDING, PADDING, PADDING, PADDING);
		final LineBorder border2 = new LineBorder(Color.BLACK);
		final CompoundBorder compBorder = new CompoundBorder(border2, border1);

		final GridBagConstraints constr = new GridBagConstraints();
		constr.fill = GridBagConstraints.BOTH;

		int crtRow = 0;

		// New integration project
		constr.weightx = 0.2;
		constr.weighty = 0;
		constr.gridx = 0;
		constr.gridy = crtRow;
		final JButton newProjBtn = createButton("New Integration Project",
				"./resources/integrate.png", "newproj");
		add(newProjBtn, constr);

		constr.weightx = 0.8;
		constr.weighty = 0;
		constr.gridx = 1;
		constr.gridy = crtRow;
		final JLabel newProjText = new JLabel("Start a new database schema integration project.");
		newProjText.setLabelFor(newProjBtn);
		newProjText.setBorder(compBorder);
		newProjText.setOpaque(false);
		add(newProjText, constr);
		crtRow++;

		// Edit configuration
		constr.weightx = 0.2;
		constr.weighty = 0;
		constr.gridx = 0;
		constr.gridy = crtRow;
		final JButton configBtn = createButton("Edit Configuration", "./resources/config_big.png",
				"edit");
		add(configBtn, constr);

		constr.weightx = 0.8;
		constr.weighty = 0;
		constr.gridx = 1;
		constr.gridy = crtRow;
		final JLabel configText = new JLabel(
				"View and edit the configuration file for the AutoMed repository.");
		configText.setLabelFor(configBtn);
		configText.setBorder(compBorder);
		configText.setOpaque(false);
		add(configText, constr);
		crtRow++;

		// Add new source
		constr.weightx = 0.2;
		constr.weighty = 0;
		constr.gridx = 0;
		constr.gridy = crtRow;
		final JButton addSrcBtn = createButton("Add Source", "./resources/add_src_big.png", "add");
		add(addSrcBtn, constr);

		constr.weightx = 0.8;
		constr.weighty = 0;
		constr.gridx = 1;
		constr.gridy = crtRow;
		final JLabel addSrcText = new JLabel(
				"Add a new source to the AutoMed repository. This source can later be used in the integration tool.");
		addSrcText.setLabelFor(addSrcBtn);
		addSrcText.setBorder(compBorder);
		add(addSrcText, constr);
		crtRow++;
	}

	/**
	 * Create a simple button with text and an icon
	 * 
	 * @param text
	 *            Text to display on button
	 * @param icon
	 *            Icon to display on button
	 * @return Button with the specitied properties
	 */
	private JButton createButton(final String text, final String icon, final String name) {
		final JButton button = new JButton();
		button.setLayout(new GridLayout(1, 1));
		button.setName(name);

		final JLabel label = new JLabel(text, new ImageIcon(icon), SwingConstants.CENTER);
		label.setVerticalTextPosition(SwingConstants.BOTTOM);
		label.setHorizontalTextPosition(SwingConstants.CENTER);
		button.add(label, BorderLayout.CENTER);

		button.addActionListener(this);
		button.setBackground(Color.WHITE);

		final Border line = new LineBorder(Color.BLACK);
		final Border margin = new EmptyBorder(5, 15, 5, 15);
		final Border compound = new CompoundBorder(line, margin);
		button.setBorder(compound);

		return button;
	}

	@Override
	public void actionPerformed(final ActionEvent arg0) {
		final JButton button = (JButton) arg0.getSource();

		if (button.getName().equals("newproj")) {
			// Start a new integration project
			parentFrame.open(ApplicationSections.PROJECT, new Object[] {}, "project"
					+ (MainFrame.projectCount++), new ImageIcon("./resources/integrate_icon.png"),
					"Pre-integration: Choose Schemas & Define Integration Strategy",
					"Drag & drop schema names to add them to the strategy wizard");
		}

		if (button.getName().equals("edit")) {
			try {
				Utils.openConfiguration();
			} catch (IOException e) {
				JOptionPane.showMessageDialog(this,
						"Could not open the AutoMed configuration file!", "Error",
						JOptionPane.ERROR_MESSAGE);
			}
		}

		if (button.getName().equals("add")) {
			// Add a new source in the AutoMed repository
			final AddSourceDialog addSrcFrame = new AddSourceDialog(parentFrame);
			addSrcFrame.setVisible(true);
		}

	}
}
